package com.lm.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lm.mapper.auto.InforMapper;
import com.lm.model.auto.Infor;
import com.lm.service.InforService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 达瓦里氏
 * @since 2021-12-09
 */
@RestController
@RequestMapping("/infor")
@CrossOrigin
public class InforController {

    @Resource
    private InforService inforService;

//    @Resource
//    private InforMapper inforMapper;

    /*查询资讯*/
    @GetMapping("/getInfor")
    public List<Infor> getInfor(){ return inforService.list(); }

    /*通过id查询资讯*/
    @GetMapping("/getInforById/{id}")
    public Infor getInforById(@PathVariable("id") int id){
        return inforService.getById(id);
    }

    /*修改资讯*/
    @PostMapping("/updateInfor")
    public void updateInfor(@RequestBody Infor infor){ inforService.updateById(infor); }

    /*添加资讯*/
    @PostMapping("/setInfor")
    public void setInfor(@RequestBody Infor infor){ inforService.save(infor); }

    /*删除资讯*/
    @PostMapping("/deletedById/{id}")
    public boolean deleted(@PathVariable("id") int id){ return inforService.removeById(id); }

    /*图片上传*/
    @PostMapping("/elupload")
    public String elUpload(MultipartFile file){
        String fileName = file.getOriginalFilename();
        File dest = new File("D:\\project\\hao\\hao-vue\\static\\img\\" + fileName);
        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败！";
    }

    /*模糊查询资讯数据*/
    @PostMapping("/selectInfo/{title}")
    public List<Map<String, Object>> selectInfo(@PathVariable("title") String value){
        System.out.println(value);
        QueryWrapper<Infor> queryWrapper = new QueryWrapper<>();
        /*核心模糊算法*/
        queryWrapper.like("title",value).or().like("content",value).or().like("name",value);
        List<Map<String, Object>> maps = inforService.listMaps(queryWrapper);
        return maps;
    }

    /*分类查询数据*/
    @GetMapping("/queryById/{id}")
    public List<Map<String, Object>> queryById(@PathVariable("id") int id){
        QueryWrapper<Infor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_id",id);
        List<Map<String, Object>> maps = inforService.listMaps(queryWrapper);
        return maps;
    }
}
